package common

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	commonPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.myscrm.cn/golang/stark/v4"
)

type verifyCodeRepository struct {
}

func NewVerifyCodeRepository() VerifyCodeRepositoryIface {
	return &verifyCodeRepository{}
}

// GetCaptcha 获取验证码
func (v *verifyCodeRepository) GetCaptcha(ctx context.Context, request *commonPb.GetCaptchaRequest) (*commonPb.GetCaptchaResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := commonPb.NewVerifyCodeServiceClient(conn)
	resp, err := client.GetCaptcha(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc GetCaptcha request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// SendSms 发送短信验证码
func (v *verifyCodeRepository) SendSms(ctx context.Context, request *commonPb.SendSmsRequest) (*commonPb.SendSmsResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := commonPb.NewVerifyCodeServiceClient(conn)
	resp, err := client.SendSms(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "call grpc SendSms request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}
